// pages/shopMenu/shopMenu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: "",
    shopNum: 0,
    currentId: 0,
    headerStyle: "",
    typeList: [],
    list:[]
  },
  onLoad(options) {
    this.getCenterPosition(),
      wx.getStorage({
        key: "typeRecord",
        success: res => {
          let tmp = res.data
          this.setData({
            nav:tmp.nav
          })
          // 左侧栏
          wx.cloud.callFunction({
            name: "getType",
            data: {
              id: tmp.typeId + 1
            }
          }).then(res => {
            // console.log(res.result.data)
            let tlist = res.result.data
            // let proList = []
            for (const menu of tlist) {
              menu.proList = []
            }
            // tmp.push(proList)
            // console.log(tlist)
            // 右侧商品
            wx.cloud.callFunction({
              name: "getShop",
              data: {
                id: tmp.typeId + 1,
              }
            }).then(res => {
              let index = res.result.data
              let menuList = this.data.typeList
              // console.log(res.result.data)
              for (const menu of menuList) {
                for (const pro of index) {
                  if(pro.info_id == (menu._id + 1)){
                    menu.proList.push(pro)
                  }
                }
              }
              this.setData({
                list:menuList
              })
              console.log(this.data.list[1].proList[1])
            })
            this.setData({
              typeList: tlist
            })
            console.log(this.data.typeList)
          })


          this.setData({
            type: tmp
          })
        }
      })
  },
  getCenterPosition() {
    // 获取胶囊的样式内容
    let {
      top,
      height
    } = wx.getMenuButtonBoundingClientRect();
    // console.log(wx.getMenuButtonBoundingClientRect())
    let centerPosition = top + height / 2;
    let headerStyle = "margin-top:calc(" + centerPosition + "px - 30rpx";
    this.setData({
      headerStyle: headerStyle
    })
  },
  // 点击选中左边栏
  leftClick(event) {
    let {
      tid
    } = event.currentTarget.dataset
    this.setData({
      currentId: tid
    })
  },
  addCout(event) {
    let {
      tid,
      sid
    } = event.currentTarget.dataset
    // console.log(tid,sid)
    let newCount = this.data.typeList[tid].shopList[sid].count
    let newList = this.data.typeList
    newCount++
    newList[tid].shopList[sid].count = newCount
    this.setData({
      typeList: newList
    })
    wx.showToast({
      title: '商品添加成功',
      icon: 'none',
      duration: 2000
    })
    this.sumShop()
  },
  // 统计商品
  sumShop() {
    let shop = this.data.typeList
    let count = 0
    for (const model of shop) {
      for (const project of model.shopList) {
        if (project.count > 0) {
          count += project.count
        }
      }
    }
    this.setData({
      shopNum: count
    })
  },
  jump() {
    wx.navigateTo({
      url: '/pages/shopDetails/shopDetails',
    })
  },
  ref() {
    wx.switchTab({
      url: '/pages/shop/shop',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // wx.getStorage({
    //   key: "typeRecord",
    //   success: res => {
    //     let tmp = res.data
    //       this.setData({
    //         nav:tmp.tmp
    //       })
    //     // console.log(tmp)
    //     // 左侧栏
    //     wx.cloud.callFunction({
    //       name: "getType",
    //       data: {
    //         id: tmp + 1
    //       }
    //     }).then(res => {
    //       // console.log(res.result.data)
    //       let tlist = res.result.data
    //       // let proList = []
    //       for (const menu of tlist) {
    //         menu.proList = []
    //       }
    //       // tmp.push(proList)
    //       // console.log(tlist)
    //       // 右侧商品
    //       wx.cloud.callFunction({
    //         name: "getShop",
    //         data: {
    //           id: tmp + 1,
    //         }
    //       }).then(res => {
    //         let index = res.result.data
    //         let menuList = this.data.typeList
    //         // console.log(res.result.data)
    //         for (const menu of menuList) {
    //           for (const pro of index) {
    //             if(pro.info_id == (menu._id + 1)){
    //               menu.proList.push(pro)
    //             }
    //           }
    //         }
    //         console.log(menuList)
    //       })
    //       this.setData({
    //         typeList: tlist
    //       })
    //     })


    //     this.setData({
    //       type: tmp
    //     })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})